var id  = 0;
var pageFrom = "11";
var mainCallType = 0;
//var commitTo = 11;
//var commitTo = 18;//freeswitchCall专用
var commitTo = 5;//freeswitchCall专用,需要推数据到保利
var createUserId = 11;
var customerName = "李四"
var objectId = 0;
var lastMsg = "";
var sessionId = "";
var interruptCallUrl = callService+"/freeswitch/interruptCall";


$("#callBtn").click(function(){
	id = Math.round(Math.random()*1000);
	objectId = Math.round(Math.random()*1000);
	var callParam = {};		
	callParam["mainCallPhone"] = $("#mainCallPhone").val();
	callParam["targetCallPhone"] = $("#targetCallPhone").val();
	callParam["id"] = id;
	callParam["pageFrom"] = pageFrom;
	callParam["commitTo"] = commitTo;
	callParam["mainCallType"] = mainCallType;
	callParam["createUserId"] = createUserId;
	callParam["customerName"] = customerName;
	callParam["city"] = 1;
	console.log(JSON.stringify(callParam));
	callout(callParam);
});


function callbackPC(data) {
   var messageData = JSON.parse(data);
   //当判断传递过来的数据 dataID以及是pageFrom_PC一致情况下，才做接收处理
   /*console.log("token:" + messageData["machineFrom"] + "_" + token);*/
   if (messageData["dataId"] == id) {
	   if (lastMsg != "" && (messageData["dataId"] == lastMsg.dataId && messageData["msgType"] == lastMsg.msgType)) {                  
			console.log("==========重复数据不做处理==========");
			return;                    
        }
        lastMsg = messageData;
		console.log("onMessage-> " + data.value + new Date());
		callPhoneManager(messageData);
    }
}


//用于接收通话返回的情况
function callPhoneManager(dataObj) {

    var msgType = dataObj["msgType"];    

    desc = dataObj["desc"];
    console.log("=======" + msgType + "=========");
    switch (msgType) {
        case 'COMMIT_SUCCESS':
			sessionId =  dataObj["sessionId"];
            $("#txResult").val($("#txResult").val()+"正为您发起外呼操作\n");
            /*outgoingRing();
            timeOutTask = setTimeout('connectTimeOut()', 50000);*/
            $("#callBtn").css("display","none");
            break;       
        case 'MAIN_ANSWERED'://主叫应答
            break;       
        case 'TARGET_ANSWERED'://被叫应答                        
			//$("#txResult").val($("#txResult").val()+"通话中\n");
            $("#txResult").val($("#txResult").val()+"已为您接通客户\n");
            break;
        case 'MAIN_DISCONNECTED'://主叫结束通话                        
            $("#txResult").val($("#txResult").val()+"通话结束!\n");
            initBtnAndsounds();
            break;
        case 'TARGET_DISCONNECTED'://被叫结束通话
            $("#txResult").val($("#txResult").val()+"客户结束了通话!\n");
            isCallEnd = true;
            initBtnAndsounds();
            break;
        case 'MAIN_FAILED'://主叫失败
            $("#txResult").val($("#txResult").val()+"无法呼叫您的号码...\n");
            isCallEnd = true;
            $("input[name=customerFollowCategory][value='nor']").attr("checked", true);//自动匹配“无人接通”
            initBtnAndsounds();
            break;
        case 'TARGET_FAILED'://被叫失败            
            $("#txResult").val($("#txResult").val()+"无法接通客户...\n");
            isCallEnd = true;
            initBtnAndsounds();
            break;
        case 'COMMIT_FAILED'://提交失败            
			$("#txResult").val($("#txResult").val()+"外呼失败\n");
            initBtnAndsounds();
            break;
        case 'EXCEPTION'://异常            
			$("#txResult").val($("#txResult").val()+desc+"\n");
            initBtnAndsounds();
            break;   
        default:
            break;
    }
}


//挂断操作
function ajaxInterrupt() {
    /*var isAsync = true;// 是否异步操作
                //sessionId
                $.ajax({
                    type: "get",
                    async: true,
                    url: interruptCallUrl + "?" + "sessionId=" + sessionId + "&autoInterrupt=false",
                    dataType: "jsonp",//数据类型为jsonp
                    jsonp: "jsonpCallBack",//服务端用于接收callback调用的function名的参数
                    success: function (data) {
                        if (data.code > 0) {//正常的结束
                            console.log("挂断成功");
                            $("#txResult").val($("#txResult").val()+"挂断成功\n");
                            initBtnAndsounds();
            } else {
				$("#txResult").val($("#txResult").val()+"挂断操作失败,请稍候再重试\n");                
            }
        },
        error: function () {
            console.log("发送http挂断操作失败");
        }
    });*/
    session.bye();
}



 loadVersion();//加载浏览器版本是否可以进行云呼操作标识
 function loadVersion(){
    // if(!((/chrome\/(\d+)/.test(navigator.appVersion.toLowerCase())) && RegExp.$1 >= 29)) {
    //     alert("当前浏览器版本不能进行外呼操作");
    // }else{
		connWS();
	//}
}

/**
 * 停止铃声,按钮显示
 */
function initBtnAndsounds() {
    $("#callBtn").css("display","inline-block");
    outgoingStop();//停止铃声
}